package ltd.followkba.comment;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import ltd.followkba.comment.utils.CodeBase64;

import static ltd.advskin.MSKIN.MSTRING;

public class KgDfcf {

    private int gRunThread;
    public void writeText(){
        MSTRING("modifyInfo A "+gRunThread);
        if (gRunThread == 0){
            gRunThread = 1;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String  code = "002536";
                    String  title = "飞龙有妖相吗?";
                    String text = "特斯拉大涨、飞龙拿下订单这么多利好不封一字都怪";
//                    code = "300378";
//                    title = "鼎捷软件";
//                    text = "鼎捷软件能为工业企业带来利润，就是实实在在的利好！";

                    WriteToSvr(HTTP_POST, "/interface/GetData.aspx", code, title, text);
//"param=id%3D903907390%26huifuid%3D%26text%3D%25E6%2594%25AF%25E6%258C%2581%25E6%2594%25AF%25E6%258C%2581%26t_type%3D8%26pic%3D%26yzm_id%3D%26yzm%3D&plat=pc&AudioUrl=replyopt%2Fapi%2FReply%2FReplyArticleWeb&env=2");
                    gRunThread = 0;
                }
            }).start();
        }
    }
    public final static int HTTP_GET = 0x01;
    public final static int HTTP_POST = 0x02;
    public final static int POST_PUT = 0x04;
    public final static int POST_DELETE = 0x08;
    public final static int GET_REMIND = 0x10;
    public final static int NO_LOGIN = 0x40;
    public final static int HAVE_SONG_USER_MIN = 10011000;
    public final static int HAVE_SONG_USER_MAX = 13000000;
    public String gsUserID = "";
    public String gsToken = "";
    public String gsMixAccount = "";
    public String gsAuthSsid = "";
    public String gsLongitude = "0";
    public String gsLatitude = "0";
    public String mDeviceMac = "64%3Acc%3A2e%3A4d%3Ad9%3A94";
    public String mUserAgent = "Dalvik/1.6.0 (Linux; U; Android 4.3; GT-N7102 Build/JSS15J)";
    public String gs_cli_model = "GT-N7102";
    public String gs_cli_uniqid = "8543f0783d111f37";
    public byte NetworkError;

    public String WriteToSvr(int type, String destUrl, String code, String title, String text) {
        String gsLocal_1 = "";
        BufferedInputStream bis = null;
        HttpURLConnection mHttpUrl=null;
        //    URLConnection mHttpUrl=null;
        URL url = null;
        byte[] gLocal_Buffer = new byte[0x100000+1460];
        String param = String.format("http://guba.eastmoney.com/news_soft.aspx?code=%s", code);
//        MSTRING(String.format("WriteToSvr A %s %s", destUrl, param));
        String cookie1 = "web_usr_addinfo=%7b%22CName%22:%22free_free%22%2c%22CVer%22:%229.1.0.9115%22%2c%22GUID%22:%223067e897e32653cd33f2390845d5027f9c3bd55865c4d100713c1caf7e41f9b5483eb478%22%2c%22IsCH%22:true%2c%22OSRes%22:%221920*1080%22%2c%22OSVer%22:%22Windows7%22%7d; alias=%e5%8f%a4%e6%b5%b7%e6%b6%9b%e4%bb%8a; c1=1d7c5cf54484dc74174c6f41e98be6e634d0d464d6a0c9275cc49ad50091bc4f; c2=2368b4b07bd5c66b4c931c497130efdfb9242945ae259af432bb22a0fb3ff61a549771da85376e562a62a72bc0f9b626e57744d9b617a4b700df2b4f6069248645703aa5f4515150; cid=3682be4c621ef585f1e09b745a874565; ct=Ly_VC4Cu0EPhGNRiz87mzuI4Sx9fIK_bLqz5zwDelrbxsyppYkc9kqqQPF-1JFvNF565mDjT2_K5WBEMoCnU5OgechS7L4ozlEir9mpf3OkLHqiNMvO3I3jqtnAzzhD5maDuYKWxmFxeEJhK591mbUdXdPCive89jQwQgh-aIxg; hasfund=0; hashuawei=0; hasjinritoutiao=0; hasqq=0; hassecurities=0; hassina=0; hastaobao=0; hasweixin=0; haszaker=0; influstar=5; influval=318; isnonrealnameuser=0; pi=7109213132245068%3b%3b%e5%8f%a4%e6%b5%b7%e6%b6%9b%e4%bb%8a%3b9JkbdFEKdyX3wYSj3belZxC3lpo6krAF%2bbY8eV8t0P6Mou17fBa9RMmH3sWhSryJHsxU8x9vMNNNePfYL%2bMD2eZeX2Jo7t%2f1JH6J5CUXcuQoxek3af89Uj8rniKhRIcXIv%2fhArKWsLYhsZTigD9NMpB4kBgqcwgFMbilklVcUsk1i7Fr78gpGZpbA6g6Rp1AySdH8kD2%3bhlzNGAxuDUwkY%2boRFuyuH3s2EZUJr0QT492RiN5SUH5s6uoLfjYuhEy%2f6CmxrknL009cxOdiAEr9rpv2vICq6ZdalgHuMZYejFGoXzmJU%2fi52B0%2bRFZzEKu2qj1sqccbq5R8MWC2FM11V1ivfdh9JQi86f%2fLNw%3d%3d; sid=117445721; sso=reqtp%3d1%26cookie1%3d1d7c5cf54484dc74174c6f41e98be6e634d0d464d6a0c9275cc49ad50091bc4f%26cookie2%3d2368b4b07bd5c66b4c931c497130efdfb9242945ae259af432bb22a0fb3ff61a549771da85376e562a62a72bc0f9b626e57744d9b617a4b700df2b4f6069248645703aa5f4515150; tag=0; ucity=; ugender=0; uid=7109213132245068; uintroduce=%e4%bb%b7%e5%80%bc%e6%8a%95%e8%b5%84%ef%bc%8c%e6%b2%a1%e6%8a%8a%e6%8f%a1%e7%9a%84%e8%82%a1%e7%a5%a8%e7%bb%9d%e4%b8%8d%e9%87%8d%e4%bb%93%e5%81%9a%e3%80%82%e7%9c%8b%e5%a5%bd%e7%9a%84%e5%85%ac%e5%8f%b8%e5%8f%af%e4%bb%a5%e9%95%bf%e6%9c%9f%e5%81%9a%e8%82%a1%e4%b8%9c%ef%bc%81; umobphoneacted=1; uname=; uprovince=; ut=FobyicMgeV75Gq1SORdNmPkD3wqJeUnYsMMuhN1-RBbuuyA7JIR9-tAbojBAS_0QQc8e-VMG9oC8MHL05jmq2BVFiZVGsEMOFancRYIzwAhMaE2aeV00JWJ4RH-PlhZce-OOC0B5eEXCWODr6HiQnSHrHR39ZBM6fZJGbptJHIu9jQnCKKxQVHmdzQAHCPFQbDUldM0xZLmwNonOG1VI-2XS4suM6RuyYtYWSscRm8Wt3XiYnSLGOtrlTkBDUzgBQdVLVX_pLiysoQ46f6ZkATonYBUKADnoVS-0mQTo1lzusKlZ--cmRWnL79nkcpdN-0IYj9ApzksJUe4rgQSKdKTyu0ygKuEQ; vflag=0; vtitle=; vtype=; vtypestatus=; uidal=7109213132245068%e5%8f%a4%e6%b5%b7%e6%b6%9b%e4%bb%8a; uniqueid=3067e897e32653cd33f2390845d5027f9c3bd55865c4d100713c1caf7e41f9b5483eb478; st_si=54494027972387; st_pvi=71395411655557; st_sp=2020-02-09%2009%3A52%3A30; st_inirUrl=http%3A%2F%2Fguba.eastmoney.com%2Fnews_soft.aspx; st_sn=6; st_psi=20200217093058382-117001304473-5258814640; st_asi=delete; qgqp_b_id=4a0b7d292a2f828d882936467c426e9d";
        String cookie2 = "st_pvi=71395411655557; st_sp=2020-02-09%2009%3A52%3A30; st_inirUrl=http%3A%2F%2Fguba.eastmoney.com%2Fnews_soft.aspx; web_usr_addinfo=%7b%22CName%22:%22free_free%22%2c%22CVer%22:%229.2.0.9123%22%2c%22GUID%22:%223067e897e32653cd33f2390845d5027f9c3bd55865c4d100713c1caf7e41f9b5483eb478%22%2c%22IsCH%22:true%2c%22OSRes%22:%221920*1080%22%2c%22OSVer%22:%22Windows7%22%7d; alias=%e5%8f%a4%e6%b5%b7%e6%b6%9b%e4%bb%8a; c1=a09999132130250e7201db783c74ecebc41308c580c51191a6e56b15c0ae1791; c2=74cb6713fc660b784c931c497130efdfb9242945ae259af4d2bd060e9756e207b7d3a5463be6c7752a62a72bc0f9b626e0edf3a939ccd96700df2b4f6069248645703aa5f4515150; cid=3682be4c621ef585f1e09b745a874565; ct=h05lpfubMbaAMCj3SXEpl9wT10XddzszW8c8YA8_qnbco7Y5CBBPBMBGrw7LhcSBIXW6x2HSSvh9qj63QqVGZvdyUVa42hvw8NViiw33D3o4FXPxGAufSrQKbwdZdnDtXcWpz1LDDJyq6vJhDHdeCkABvCozJBYf11hu4XE0D5k; hasfund=0; hashuawei=0; hasjinritoutiao=0; hasqq=0; hassecurities=0; hassina=0; hastaobao=0; hasweixin=0; haszaker=0; influstar=5; influval=318; isnonrealnameuser=0; pi=7109213132245068%3b%3b%e5%8f%a4%e6%b5%b7%e6%b6%9b%e4%bb%8a%3bUUQ2V97875xaSJvMlrZFEFa1lVZLbjjXK4H8gRekUz295oOzc%2fqklhzcPtJeNntkE4hMe57MonSx98quqP26KH6lsWYDOTxY%2f4ky6NzlIIfnuGHNsdNYTwhoEEQ0qPydt1MOKSb9MecIr51wZJBbynt3hfyjmaqQTciVPKSi0RoXu2sRdkmE%2bO%2fkmH0vEZ5pp7GEmuFB%3bJxHmncIaVR7eVDC2HxnsLGQx1Pye3lXDi%2bw93LA87Al5uufVsjQC6%2bkLI%2b1qRJlGmdKZRcRoF4NbZxsMwX0T8cc%2bxWy0wxYchtIhPA9DI%2ft56jf%2baeYhQEDuuldaS%2bMu4rOGGUIBprOGdWNWhkNG8uV0%2fQKlvg%3d%3d; sid=117445721; sso=reqtp%3d1%26cookie1%3da09999132130250e7201db783c74ecebc41308c580c51191a6e56b15c0ae1791%26cookie2%3d74cb6713fc660b784c931c497130efdfb9242945ae259af4d2bd060e9756e207b7d3a5463be6c7752a62a72bc0f9b626e0edf3a939ccd96700df2b4f6069248645703aa5f4515150; tag=0; ucity=; ugender=0; uid=7109213132245068; uintroduce=%e4%bb%b7%e5%80%bc%e6%8a%95%e8%b5%84%ef%bc%8c%e6%b2%a1%e6%8a%8a%e6%8f%a1%e7%9a%84%e8%82%a1%e7%a5%a8%e7%bb%9d%e4%b8%8d%e9%87%8d%e4%bb%93%e5%81%9a%e3%80%82%e7%9c%8b%e5%a5%bd%e7%9a%84%e5%85%ac%e5%8f%b8%e5%8f%af%e4%bb%a5%e9%95%bf%e6%9c%9f%e5%81%9a%e8%82%a1%e4%b8%9c%ef%bc%81; umobphoneacted=1; uname=; uprovince=; ut=FobyicMgeV75Gq1SORdNmPkD3wqJeUnYsMMuhN1-RBbuuyA7JIR9-tAbojBAS_0QQc8e-VMG9oC8MHL05jmq2BVFiZVGsEMOFancRYIzwAhMaE2aeV00JWJ4RH-PlhZcpqeqwEGGkkdqZbIzxm62Br-Q6dzRRxwttxPGDG2nhRV8ifKvyUh606bqaaPD2sj3ipxQPTy5Vvti71FmowUcrFUZAJeUEngixoTGtcAA-VMhLN-_NMEKKC_9879kdeJZn-qgcqvRwtHmiE07H549A7tn0IQeFIRIVVrUQY6iRtWPnrg6dClN0qG1jqsVjX8z9spOvxG845ooq8Sx82OYrEG6SZiM_WyQ; vflag=0; vtitle=; vtype=; vtypestatus=; uidal=7109213132245068%e5%8f%a4%e6%b5%b7%e6%b6%9b%e4%bb%8a; uniqueid=3067e897e32653cd33f2390845d5027f9c3bd55865c4d100713c1caf7e41f9b5483eb478; st_si=17135047154694; qgqp_b_id=4a0b7d292a2f828d882936467c426e9d";
        try {
            url = new URL("http://guba.eastmoney.com" + destUrl);
            mHttpUrl = (HttpURLConnection) url.openConnection();
            mHttpUrl.setConnectTimeout(30000);
            mHttpUrl.setReadTimeout(30000);

            mHttpUrl.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
            mHttpUrl.setRequestProperty("Origin", "http://guba.eastmoney.com");
            mHttpUrl.setRequestProperty("X-Requested-With", "XMLHttpRequest");
            mHttpUrl.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            mHttpUrl.setRequestProperty("Referer", param);
            mHttpUrl.setRequestProperty("Cookie", cookie1);
            if ((type & HTTP_GET) > 0) {
                mHttpUrl.setRequestProperty("cli-mcc", "");
                mHttpUrl.setRequestProperty("cli-mnc", "");
            }
            else {
                mHttpUrl.setRequestProperty("cli-mcc", "460");
                mHttpUrl.setRequestProperty("cli-mnc", "01");
            }
            if ((type & POST_PUT) > 0) {
                mHttpUrl.setRequestProperty("X-HTTP-Method-Override", "PUT");
            }
            else if ((type & POST_DELETE) > 0) {
                mHttpUrl.setRequestProperty("X-HTTP-Method-Override", "DELETE");
            }
            mHttpUrl.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36");
// 117.184.45.228
//MLOG("HTTP_GET= 1 " + LoginFlag + " "+type);
            if ((type & GET_REMIND) > 0){
//MLOG("HTTP_GET= 2 " + LoginFlag + " "+type);
                mHttpUrl.setRequestProperty("authorize-token", CodeBase64.encodeBytes(gsMixAccount.getBytes()));
            }
            else if (((type & NO_LOGIN) == 0 && !"".equals(gsMixAccount))){
                mHttpUrl.setRequestProperty("authorize-token", CodeBase64.encodeBytes(gsMixAccount.getBytes()));
            }

            mHttpUrl.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            mHttpUrl.setRequestProperty("Connection", "Keep-Alive");
            mHttpUrl.setRequestProperty("Accept-Encoding", "gzip");

            mHttpUrl.setDoInput(true);
            if ((type & HTTP_GET) > 0){
                mHttpUrl.setDoOutput(false);
                mHttpUrl.setUseCaches(false);
                mHttpUrl.setRequestMethod("GET");
            }
            else  {
                mHttpUrl.setDoOutput(true);
                mHttpUrl.setUseCaches(false);
                mHttpUrl.setRequestMethod("POST");
                DataOutputStream dos=new DataOutputStream(mHttpUrl.getOutputStream());
                param ="param=code%3D" + code + "%26title%3D" + URLEncoder.encode(title) + "%26text%3D" + URLEncoder.encode(text);
                param += "%26yzm_id%3D%26yzm%3D%26pic%3D&plat=pc&AudioUrl=postopt%2Fapi%2Fpost%2Fpostarticleweb&env=2";
                dos.writeBytes(param);
                dos.flush();                    // 刷新对象输出流，将任何字节都写入潜在的流中（些处为ObjectOutputStream）
                dos.close();                    // 关闭流对象。此时，不能再向对象输出流写入任何数据，先前写入的数据存在于内存缓冲区中,
                MSTRING("WriteToSvr C "+param);
            }
            // 再调用下边的getInputStream()函数时才把准备好的http请求正式发送到服务器
            int responseCode = 0;
            try {
                responseCode = mHttpUrl.getResponseCode();
            } catch (IOException e) {
            }
            if (responseCode == 200 || responseCode == 201) {
                bis = new BufferedInputStream(mHttpUrl.getInputStream());     // 注意，实际发送请求的代码段就在这里
            }
            else {
                bis = new BufferedInputStream(mHttpUrl.getErrorStream());     // 注意，实际发送请求的代码段就在这里
            }

            int size = 0;
            int g4Local_1 = 0;
            while ((size = bis.read(gLocal_Buffer, g4Local_1, 1460)) != -1) {
                if (size > 0){
                    g4Local_1 += size;
                    if (g4Local_1 >= 0x100000){
                        break;
                    }
                }
            }
            bis.close();
            if (g4Local_1 > 0){
                gsLocal_1 = new String(gLocal_Buffer, 0, g4Local_1, "utf-8");
            }
            NetworkError = 0;
//            MSTRING("WriteToSvr D "+gsLocal_1);
            mHttpUrl.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
            ++NetworkError;
        }
        return gsLocal_1;
    }
}
